package generator;

import cc.rengu.redp.common.utils.EncryptUtil;
import cc.rengu.redp.common.utils.gmhelper.SM4Util;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Scanner;

public class YamlEncryptGenerator {
    public static void main(String[] args) throws NoSuchAlgorithmException,
            NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException,
            BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
        Scanner scanner = new Scanner(System.in);
        String key = "";
        while (key.length() != 16 || !StringUtils.isAlphanumeric(key)) {
            System.out.println(("请输入启动密钥（16位数字和字符）" + "："));
            key = scanner.next();
        }
        String iv = (new StringBuilder(key)).reverse().toString();
        System.out.println(("请输入DATA" + "："));
        String data = scanner.next();
        String enData = EncryptUtil.byteArr2HexStr(SM4Util.encrypt_CBC_Padding(key.getBytes(),iv.getBytes(), data.getBytes()));
        System.out.println("密文: \n" + enData);
    }
}
